//package com.shanwen.shopping.activity;
//
//import android.content.DialogInterface;
//import android.view.KeyEvent;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.widget.ImageView;
//import android.widget.ProgressBar;
//import android.widget.TextView;
//
//import com.classic.common.MultipleStatusView;
//import com.classic.core.activity.BaseActivity;
//import com.classic.core.log.Logger;
//import com.classic.core.utils.AppInfoUtil;
//import com.classic.core.utils.DataUtil;
//import com.classic.core.utils.DoubleClickExitHelper;
//import com.classic.core.utils.NetworkUtil;
//import com.classic.core.utils.PackageUtil;
//import com.classic.core.utils.SDcardUtil;
//import com.classic.core.utils.ToastUtil;
//import com.classic.okhttp.base.OkHttpUtils;
//import com.classic.okhttp.base.callback.FileCallBack;
//import com.flyco.tablayout.CommonTabLayout;
//import com.flyco.tablayout.listener.CustomTabEntity;
//import com.flyco.tablayout.listener.OnTabSelectListener;
//import com.shanwen.shopping.R;
//import com.shanwen.shopping.consts.Consts;
//import com.shanwen.shopping.dao.ShoppingCartHelper;
//import com.shanwen.shopping.dialog.CommonDialog;
//import com.shanwen.shopping.entity.AppInfo;
//import com.shanwen.shopping.entity.BannerInfo;
//import com.shanwen.shopping.entity.ModuleInfo;
//import com.shanwen.shopping.entity.TabEntity;
//import com.shanwen.shopping.fragment.AppBaseFragment;
//import com.shanwen.shopping.fragment.MainContentList;
//import com.shanwen.shopping.http.ActionHelp;
//import com.shanwen.shopping.http.BaseCallback;
//import com.shanwen.shopping.http.ListCallback;
//import com.shanwen.shopping.http.ObjectCallback;
//import com.shanwen.shopping.utils.EventUtil;
//import com.shanwen.shopping.utils.Utils;
//import com.shanwen.shopping.view.SuperSwipeRefreshLayout;
//import com.squareup.picasso.Picasso;
//
//import org.simple.eventbus.Subscriber;
//import org.simple.eventbus.ThreadMode;
//
//import java.io.File;
//import java.util.ArrayList;
//import java.util.List;
//
//import butterknife.BindView;
//import butterknife.ButterKnife;
//import butterknife.Unbinder;
//import cn.bingoogolapple.bgabanner.BGABanner;
//
//public class MainActivity extends BaseActivity implements OnTabSelectListener, BGABanner.Adapter<ImageView, BannerInfo>
//        , BGABanner.Delegate<ImageView, BannerInfo> {
//
//    private List<BannerInfo> mBannerList;
//    //        private BannerController<Banner> mBannerController;
////    private SliderLayout mSliderLayout;
//    private CommonTabLayout mTabLayout;
//    private TextView mGoodsHome;
//    private TextView mRecharge;
//    private DoubleClickExitHelper mDoubleClickHelper;
//    @BindView(R.id.main_multiple_status_view)
//    MultipleStatusView multipleStatusView;
//
//    //    private MainContentList item0;
////    private MainContentList item1;
////    private MainContentList item2;
////    private MainContentList item3;
////    private MainContentList item4;
////    private MainContentList item5;
//    private MainContentList[] items = new MainContentList[5];
//    //    private AppBaseFragment item;
//    private List<AppBaseFragment> fragments;
//
//    private List<ModuleInfo> moduleInfos;
//    private boolean isClearShoppingCart = false;
//    private BGABanner BgaBanner;
//    private SuperSwipeRefreshLayout materialRefreshLayout;
//
//    private int curTab = 0;
//
//    //    private int[] mIconUnselectIds = {
////            R.mipmap.ic_tab_0_unselect, R.mipmap.ic_tab_1_unselect,
////            R.mipmap.ic_tab_2_unselect, R.mipmap.ic_tab_3_unselect};
////    private int[] mIconSelectIds = {
////            R.mipmap.ic_tab_0_select, R.mipmap.ic_tab_1_select,
////            R.mipmap.ic_tab_2_select, R.mipmap.ic_tab_3_select};
//    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
//    private Unbinder unbinder;
//
//    @Override
//    public int getLayoutResId() {
//        return R.layout.activity_main;
//    }
//
//    @Override
//    public void initView() {
//        super.initView();
//        mBannerList = new ArrayList<>();
//        unbinder = ButterKnife.bind(this);
//        mDoubleClickHelper = new DoubleClickExitHelper(activity);
//        multipleStatusView.setOnRetryClickListener(onRetryClickListener);
//        initContentView(multipleStatusView);
//        initLoadingView();
//        checkVersion();
//    }
//
//    private void checkVersion() {
//        if (!NetworkUtil.isNetworkAvailable(activity)) {
//            return;
//        }
//        final int currentVersionCode = AppInfoUtil.getVersionCode(this);
//        ActionHelp.qryNewVersion(this, String.valueOf(currentVersionCode), new ObjectCallback<AppInfo>() {
//            @Override
//            public void onSuccess(final AppInfo response) {
//                if (null != response) {
//                    final int newVersionCode = Integer.valueOf(response.getVersionCode2());
//                    if (newVersionCode > currentVersionCode) {
//                        final CommonDialog dialog = new CommonDialog(activity).setTitle("版本更新")
//                                .setContent("更新内容:\n" +
//                                        response.getDescInfo())
//                                .setCancelText("取消")
//                                .setConfirmText("更新")
//                                .setCanceledOnTouch(
//                                        response.getIsForce().equals("n"))
//                                .setOnClickListener(
//                                        new CommonDialog.OnClickListener() {
//                                            @Override
//                                            public void confirm() {
//                                                downloadApk(response);
//                                            }
//
//                                            @Override
//                                            public void cancel() {
//                                            }
//                                        });
//                        dialog.show();
//                    }
//                }
//            }
//
//            @Override
//            public Class<AppInfo> getType() {
//                return AppInfo.class;
//            }
//        });
//    }
//
//    private void downloadApk(final AppInfo response) {
//        final String apkName = "shopping_" + response.getVersionCode() + ".apk";
//        final File file = new File(SDcardUtil.getApkDirPath(), apkName);
//        if (file.exists()) {
//            PackageUtil.install(activity, file.getPath());
//            return;
//        }
//        OkHttpUtils.get()
//                .url(response.getFileUrl())
//                .tag(activity)
//                .build()
//                .execute(new FileCallBack(SDcardUtil.getApkDirPath(), apkName) {
//                    @Override
//                    public void onSuccess(File response) {
//                        PackageUtil.install(activity, file.getPath());
//                    }
//
//                    @Override
//                    public void onError(int code, String errorMessage) {
//                        super.onError(code, errorMessage);
//                        ToastUtil.showToast(activity, "更新失败");
//                    }
//
//                    @Override
//                    public void onHttpError(int statusCode) {
//                        super.onHttpError(statusCode);
//                        ToastUtil.showToast(activity, "更新失败");
//                    }
//                });
//    }
//
//    public void initLoadingView() {
//        if (NetworkUtil.isNetworkAvailable(activity)) {
//            multipleStatusView.showLoading();
//            getModules();
//            getBanner();
//            updateVersion();
//        } else {
//            multipleStatusView.showNoNetwork();
//        }
//    }
//
//    private View.OnClickListener onRetryClickListener = new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//            initLoadingView();
//        }
//    };
//
//    private void initContentView(View parentView) {
////        mSliderLayout = (SliderLayout) parentView.findViewById(R.id.main_slider_layout);
//        fragments = new ArrayList<>();
//        BgaBanner = (BGABanner) parentView.findViewById(R.id.BgaBanner);
//        materialRefreshLayout = (SuperSwipeRefreshLayout) parentView.findViewById(R.id.main_content_refresh_layout);
//        materialRefreshLayout.setHeaderView(createHeaderView());
//        materialRefreshLayout.setFooterView(createFooterView());
//        materialRefreshLayout.setEnabled(true);
//        materialRefreshLayout.setOnPushLoadMoreListener(new SuperSwipeRefreshLayout.OnPushLoadMoreListener() {
//            @Override
//            public void onLoadMore() {
//                footerTextView.setText("正在加载...");
//                footerImageView.setVisibility(View.GONE);
//                footerProgressBar.setVisibility(View.VISIBLE);
//                for (MainContentList item : items) {
//                    if (item != null) {
//                        item.getData(item.moduleInfo.getModuleId(), item.currentPublishTime);
//                    }
//                }
//            }
//
//            @Override
//            public void onPushDistance(int distance) {
//
//            }
//
//            @Override
//            public void onPushEnable(boolean enable) {
//                footerTextView.setText(enable ? "松开加载" : "上拉加载");
//                footerImageView.setVisibility(View.VISIBLE);
//                footerImageView.setRotation(enable ? 0 : 180);
//            }
//        });
//        materialRefreshLayout.setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {
//            @Override
//            public void onRefresh() {
//                textView.setText("正在刷新");
//                imageView.setVisibility(View.GONE);
//                progressBar.setVisibility(View.VISIBLE);
//                for (MainContentList item : items) {
//                    if (item != null) {
//                        item.currentPublishTime = 0;
//                        item.getData(item.moduleInfo.getModuleId(), item.currentPublishTime);
//                    }
//                }
//                initLoadingView();
//            }
//
//            @Override
//            public void onPullDistance(int distance) {
//
//            }
//
//            @Override
//            public void onPullEnable(boolean enable) {
//                textView.setText(enable ? "松开刷新" : "下拉刷新");
//                imageView.setVisibility(View.VISIBLE);
//                imageView.setRotation(enable ? 180 : 0);
//            }
//        });
////        mTabLayout = (CommonTabLayout) parentView.findViewById(R.id.main_tab_layout);
//        mGoodsHome = (TextView) parentView.findViewById(R.id.main_goods_home);
//        mRecharge = (TextView) parentView.findViewById(R.id.main_recharge);
//        mGoodsHome.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(activity, GoodsHomeActivity.class);
//            }
//        });
//        mRecharge.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(activity, RechargeActivity.class);
//            }
//        });
//        BgaBanner.setAdapter(this);
//        BgaBanner.setDelegate(this);
//
//    }
//
//    private void setTabLayout() {
//        mTabEntities.clear();
//        try {
//            for (int i = 0; i < moduleInfos.size(); i++) {
//                final String title = moduleInfos.get(i).getModuleName();
//                mTabEntities.add(new TabEntity(title));
//            }
//        } catch (Exception e) {
//            multipleStatusView.showError();
//            return;
//        }
//
//        mTabLayout.setOnTabSelectListener(this);
//        mTabLayout.setTabData(mTabEntities);
//        mTabLayout.setCurrentTab(curTab);
//        onTabSelect(curTab);
//    }
//
//
//    private ShoppingCartHelper helper;
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        if (isClearShoppingCart) {
//            //// TODO: 16/6/12
//            if (null == helper) helper = ShoppingCartHelper.getInstance(activity);
//            if (!DataUtil.isEmpty(helper.getAll())) {
//                final CommonDialog dialog = new CommonDialog(activity)
//                        .setTitle("清空购物车")
//                        .setContent("是否清空购物车?")
//                        .setCancelText("取消")
//                        .setConfirmText("清空")
//                        .setOnClickListener(new CommonDialog.OnClickListener() {
//                            @Override
//                            public void confirm() {
//                                helper.deleteAll();
//                            }
//
//                            @Override
//                            public void cancel() {
//                            }
//                        });
//                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//                    @Override
//                    public void onDismiss(DialogInterface dialog) {
//                        isClearShoppingCart = false;
//                    }
//                });
//                dialog.show();
//            }
//        }
//    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//    }
//
//    @Override
//    public void onTabSelect(int position) {
//        curTab = position;
//        ModuleInfo moduleInfo = moduleInfos.get(position);
//        items[position] = MainContentList.newInstance(moduleInfo);
//        changeFragment(R.id.main_list_layout, items[position]);
////        switch (position) {
////            case 0:
////                item0 = MainContentList.newInstance(moduleInfo);
////                changeFragment(R.id.main_list_layout, item0);
////                break;
////            case 1:
////                item1 = MainContentList.newInstance(moduleInfo);
////                changeFragment(R.id.main_list_layout, item1);
////                break;
////            case 2:
////                item2 = MainContentList.newInstance(moduleInfo);
////                changeFragment(R.id.main_list_layout, item2);
////                break;
////            case 3:
////                item3 = MainContentList.newInstance(moduleInfo);
////                changeFragment(R.id.main_list_layout, item3);
////                break;
////            case 4:
////                item4 = MainContentList.newInstance(moduleInfo);
////                changeFragment(R.id.main_list_layout, item4);
////                break;
////            case 5:
////                item5 = MainContentList.newInstance(moduleInfo);
////                changeFragment(R.id.main_list_layout, item5);
////                break;
////        }
//    }
//
//    @Override
//    public void onTabReselect(int position) {
//    }
//
//    private void getModules() {
//        ActionHelp.qryModuleList(activity, "3", 0, new ListCallback<ModuleInfo>() {
//            @Override
//            public void onSuccess(List<ModuleInfo> response) {
//                hide();
//                if (!DataUtil.isEmpty(response)) {
//                    moduleInfos = response;
//                    setTabLayout();
//                    multipleStatusView.showContent();
//                } else {
//                    multipleStatusView.showEmpty();
//                }
//            }
//
//            @Override
//            public void onError(int code, String errorMessage) {
//                super.onError(code, errorMessage);
//                hide();
//                Logger.e(code + "," + errorMessage);
//                Utils.showToast(activity, "获取新闻分类失败");
//                multipleStatusView.showError();
//            }
//
//            @Override
//            public Class<ModuleInfo> getType() {
//                return ModuleInfo.class;
//            }
//        });
//    }
//
//    private void getBanner() {
//        ActionHelp.qryEnterpriseZoneBanner(activity, "3", new ListCallback<BannerInfo>() {
//            @Override
//            public void onSuccess(List<BannerInfo> response) {
//                hide();
//                mBannerList.clear();
//                if (!DataUtil.isEmpty(response)) {
//                    mBannerList.addAll(response);
//                    BgaBanner.setData(mBannerList, null);
//                }
//            }
//
//            @Override
//            public void onError(int code, String errorMessage) {
//                super.onError(code, errorMessage);
//                hide();
//                Logger.e("获取banner失败,code:" + code + ",message:" + errorMessage);
//            }
//
//            @Override
//            public Class<BannerInfo> getType() {
//                return BannerInfo.class;
//            }
//        });
//    }
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        return mDoubleClickHelper.onKeyDown(keyCode, event);
//    }
//
//    @Override
//    public void register() {
//        EventUtil.registerEventBus(this);
//    }
//
//    @Override
//    public void unRegister() {
//        unbinder.unbind();
//        EventUtil.unRegisterEventBus(this);
//    }
//
//    @Subscriber(tag = Consts.EVENT_CLEAR_SHOPPING_CART, mode = ThreadMode.MAIN)
//    public void clearShoppingCart(String params) {
//        isClearShoppingCart = true;
//    }
//
//    private void updateVersion() {
//        ActionHelp.qryNewVersion(activity, "1000", new BaseCallback() {
//            @Override
//            public void parse(String data) {
//                hide();
//                System.out.println(data);
//            }
//
//            @Override
//            public void onError(int code, String errorMessage) {
//                hide();
//                super.onError(code, errorMessage);
//            }
//        });
//    }
//
//    @Override
//    public void fillBannerItem(BGABanner banner, ImageView itemView, BannerInfo model, int position) {
//        Picasso.with(MainActivity.this)
//                .load(model.getImgUrl())
//                .into(itemView);
//    }
//
//    @Override
//    public void onBannerItemClick(BGABanner banner, ImageView itemView, BannerInfo info, int position) {
//        switch (info.getType()) {
//            case BannerInfo.TYPE_BANNER:
//
//                break;
//            case BannerInfo.TYPE_BOOKS:
//
//                break;
//            case BannerInfo.TYPE_BOOKSURL:
//                break;
//            case BannerInfo.TYPE_URL:
//            case BannerInfo.TYPE_UR2:
//                WebViewActivity.start(activity,
//                        DataUtil.isEmpty(info.getBannerName()) ? "" : info.getBannerName(),
//                        info.getContent());
//                break;
//        }
//    }
//
//    public SuperSwipeRefreshLayout getMaterialRefreshLayout() {
//        return materialRefreshLayout;
//    }
//
//    public void hide() {
//        footerImageView.setVisibility(View.VISIBLE);
//        footerProgressBar.setVisibility(View.GONE);
//        progressBar.setVisibility(View.GONE);
//        materialRefreshLayout.setLoadMore(false);
//        materialRefreshLayout.setRefreshing(false);
//    }
//
//    // Header View
//    private ProgressBar progressBar;
//    private TextView textView;
//    private ImageView imageView;
//    // Footer View
//    private ProgressBar footerProgressBar;
//    private TextView footerTextView;
//    private ImageView footerImageView;
//
//    private View createHeaderView() {//设置头布局
//        View headerView = LayoutInflater.from(materialRefreshLayout.getContext()).inflate(R.layout.layout_head, null);
//        progressBar = (ProgressBar) headerView.findViewById(R.id.pb_view);
//        textView = (TextView) headerView.findViewById(R.id.text_view);
//        textView.setText("下拉刷新");
//        imageView = (ImageView) headerView.findViewById(R.id.image_view);
//        imageView.setVisibility(View.VISIBLE);
//        imageView.setImageResource(R.mipmap.xrefreshview_arrow);
//        progressBar.setVisibility(View.GONE);
//        return headerView;
//    }
//
//    private View createFooterView() {//设置脚布局
//        View footerView = LayoutInflater.from(materialRefreshLayout.getContext()).inflate(R.layout.layout_footer, null);
//        footerProgressBar = (ProgressBar) footerView.findViewById(R.id.footer_pb_view);
//        footerImageView = (ImageView) footerView.findViewById(R.id.footer_image_view);
//        footerTextView = (TextView) footerView.findViewById(R.id.footer_text_view);
//        footerProgressBar.setVisibility(View.GONE);
//        footerImageView.setVisibility(View.VISIBLE);
//        footerImageView.setImageResource(R.mipmap.xrefreshview_arrow);
//        footerTextView.setText("上拉加载更多...");
//        return footerView;
//    }
//}
